library(tidyverse)
library(dplyr)
library(glue)
library(ggplot2)
library(here)
library(readxl)
library(stringr)
library(scales)
library(forcats)
library(plotly)
library(tmap)
library(sf)
library(leaflet)
library(shiny)
# data from world bank
pop <- read.csv(here("data/population_data.csv"))
# cleaning
names(pop) <- tolower(names(pop))
pop_tidy <- pop %>%
select("series.name",
"country.name",
"country.code",
5:14) %>%
pivot_longer(cols = 4:13,
names_to = "year",
values_to = "population") %>%
rename(sex = "series.name") %>%
mutate(sex = str_remove(sex, "Population,[:blank:]"),
year = str_remove(year, "x[0-9]+..yr"),
year = str_remove(year, "\\.$"),
year = str_trim(year, side = "right"),
population = as.numeric(population))
# plot it
pop_tidy$tooltip <-
glue_data(pop_tidy,
"Sex: {sex}",
"\nYear: {year}",
"\nPopulation: {comma(population, accuracy = 1)}")
ggplotly(
pop_tidy %>%
filter(country.name == "World") %>%
ggplot(aes(x = year,
y = population,
group = sex,
color = sex)) +
geom_line(aes(text = tooltip)) +
scale_y_continuous(labels = comma) +
scale_color_brewer(palette = "Set1") +
theme_light() +
# theme(panel.grid.major.x = element_blank()) +
labs(x = NULL,
title = "Population by sex and year",
caption = "Source: World Bank"),
tooltip = "text"
) %>%
config(displayModeBar = FALSE,
displaylogo = FALSE)
# data from united nations, spreadsheet contains global and regional data by sex and age https://population.un.org/wpp/DataQuery/
pop_age <- read_excel(here("data/PopulationAgeSex-20211213064029.xlsx"), sheet = 2, range = "B2:Z17")
# cleaning
names(pop_age) <- tolower(names(pop_age))
pop_age_long <-
pop_age %>%
pivot_longer(cols = 5:25,
names_to = "age",
values_to = "population")
# make male population go to the left of x axis, thus negative sign
pop_age_long$population <- ifelse(pop_age_long$sex == "Male", -1*pop_age_long$population, pop_age_long$population)
# plot it
pop_age_long_20 <- pop_age_long %>%
filter(sex %in% c("Female", "Male"),
time == "2020")
pop_age_long_20$tooltip <-
glue_data(pop_age_long_20,
"Sex: {sex}",
"\nPopulation (thousands): {ifelse(pop_age_long_20$sex == 'Male', comma(-(population), accuracy = 1), comma(population, accuracy = 1))}")
ggplotly(
pop_age_long_20 %>%
ggplot(aes(x = fct_inorder(age), # maintain order of age
y = population,
fill = sex)) +
geom_bar(data = subset(pop_age_long_20, sex == "Female"),
aes(text = tooltip),
stat = "identity") +
geom_bar(data = subset(pop_age_long_20, sex == "Male"),
aes(text = tooltip),
stat = "identity") +
scale_y_continuous(breaks = seq(-400000, 400000, 100000),
labels = paste0(as.character(c(seq(400, 0, -100), seq(100, 400, 100))), "m")) +
coord_flip() +
scale_fill_brewer(palette = "Set1") +
theme_light() +
labs(x = "Age",
y = "Population",
fill = "Sex"),
tooltip = "text"
) %>%
layout(title = list(text = paste0("Population by sex and age",
"<br>",
"<sup>",
"Year: 2020",
"</sup>"),
font = list(size = 12)),
annotations = list(x = 0.1,
y = -0.1,
text = "Source: United Nations",
showarrow = FALSE,
xref = "paper",
yref = "paper",
xanchor = "right",
yanchor = "auto",
xshift = 0,
yshift = 0,
font = list(size = 8)
)) %>%
config(displayModeBar = FALSE,
displaylogo = FALSE)
labor <- read_excel(here("data/labor_force_world_bank.xlsx"))
# cleaning
names(labor) <- tolower(names(labor))
names(labor) <- gsub(" ", "_", names(labor))
labor_tidy <- labor %>%
select(-c(series_code, `2020_[yr2020]`)) %>%
pivot_longer(cols = 4:33,
names_to = "year",
values_to = "percentage") %>%
mutate(percentage = as.numeric(percentage),
year = str_remove(year, "_\\[yr[0-9]+\\]"))
# plot it
labor_tidy_world <- labor_tidy %>%
filter(country_name == "World",
series_name == "Labor force, female (% of total labor force)")
labor_tidy_world$tooltip <-
glue_data(labor_tidy_world,
"Year: {year}",
"\nPercentage: {paste(round(percentage, 2), '%', sep = '')}")
ggplotly(
labor_tidy_world %>%
ggplot(aes(x = year,
y = percentage,
group = 1)) +
geom_line(aes(text = tooltip),
color = "red") +
geom_point(aes(text = tooltip),
color = "red",
size = 0.7) +
theme_light() +
theme(axis.text.x = element_text(angle = 90, size = 6),
panel.grid.major.x = element_blank()) +
labs(x = NULL,
y = "Percentage %",
title = "Female labor force percentage by year"),
tooltip = "text"
) %>%
layout(annotations = list(x = 0.1,
y = -0.12,
text = "Source: United Nations",
showarrow = FALSE,
xref = "paper",
yref = "paper",
xanchor = "right",
yanchor = "auto",
xshift = 0,
yshift = 0,
font = list(size = 8)
)) %>%
config(displayModeBar = FALSE,
displaylogo = FALSE)
# get world map and coordinates for each countries
data("World")
coords <- World %>%
select("iso_a3",
"name",
"geometry") %>%
rename(country_code = "iso_a3")
labor_tidy_coords <- labor_tidy %>%
filter(series_name == "Labor force, female (% of total labor force)") %>%
right_join(coords,
by = "country_code") %>%
mutate(year = as.numeric(year),
percentage = round(percentage, 2))
# leaflet map
labor_leaflet <- labor_tidy_coords %>%
filter(year == "2019") %>%
slice(1:169) %>%
st_as_sf()
palette <- colorBin(palette = "viridis",
domain = labor_leaflet$percentage,
bins = 5,
na.color ="transparent",
reverse = TRUE)
content <- paste("Country:", labor_leaflet$country_name,
"<br> Year:", labor_leaflet$year,
"<br> Percentage:", labor_leaflet$percentage)
text <- paste("Country: ", labor_leaflet$country_name,
"<br>",
"Female labor force: ", paste(labor_leaflet$percentage, "%", sep = "")) %>%
lapply(htmltools::HTML)
labor_leaflet %>%
leaflet() %>%
setView(lat = 0, lng = 0, zoom = 1) %>%
addTiles() %>%
addPolygons(
fillColor = ~ palette(percentage),
weight = 0.5,
opacity = 1,
color = "white",
smoothFactor = 0.2,
fillOpacity = 1,
label = text,
highlight = highlightOptions(
color = "#666",
weight = 2,
bringToFront = TRUE,
opacity = 1)
) %>%
addLegend(
pal = palette,
values = labor_leaflet$percentage,
opacity = 0.7,
title = paste("Female labor force 2019",
"<br>",
"%"),
position = "bottomleft"
)
# leaflet shiny app
labor_shiny <- labor_tidy_coords %>%
slice(1:5070) %>%
st_as_sf()
ui <- fluidPage(
sliderInput("range",
"year",
min = min(labor_shiny$year),
max = max(labor_shiny$year),
value = min(labor_shiny$year),
step = 1),
leafletOutput("labormap")
)
server <- function(input, output, session) {
output$labormap <- renderLeaflet({
labor_shiny %>%
filter(year == input$range) %>%
leaflet() %>%
setView(lat = 0, lng = 0, zoom = 1) %>%
addTiles() %>%
addPolygons(
fillColor = ~ palette(percentage),
weight = 0.5,
opacity = 1,
color = "white",
smoothFactor = 0.2,
fillOpacity = 1,
label = text,
highlight = highlightOptions(
color = "#666",
weight = 2,
bringToFront = TRUE,
opacity = 1)
) %>%
addLegend(
pal = palette,
values = labor_shiny$percentage,
opacity = 0.7,
title = paste("Female labor force 2019",
"<br>",
"%"),
position = "bottomleft"
)
})
}
shinyApp(ui, server)
# world development indicators from world bank https://datacatalog.worldbank.org/search/dataset/0037712
wdi <- read.csv(here("data/WDIData.csv"))
employment <- wdi %>%
filter(Indicator.Name %in% c("Employment to population ratio, 15+, female (%) (modeled ILO estimate)",
"Employment to population ratio, 15+, male (%) (modeled ILO estimate)",
"Employment to population ratio, ages 15-24, female (%) (modeled ILO estimate)",
"Employment to population ratio, ages 15-24, female (%) (national estimate)"))
wbl_df <- read_xlsx(here("data/WBL50YearPanelDetailsWeb21Feb2021.xlsx"))
indicators <- read.csv(here("data/API_17_DS2_en_csv_v2_3371618.csv"), skip = 4)
The WBL (The Women Business and the Law Index) index measures the economic empowerment of women. It is based on the countries’ formal laws and regulations that have a bearing on women’s economic participation, covering eight areas such as parenthood, equality of pay. It tracks how laws affect women at different stages in their working lives and focusing on those laws applicable in the main business city (“Women Business and the Law (WBL) Index 2020” 2020,)
# cleaning
seats <- indicators %>%
filter(Indicator.Name == "Proportion of seats held by women in national parliaments (%)",
Country.Name %in% c("Africa",
"East Asia & Pacific",
"Europe & Central Asia",
"Latin America & the Caribbean",
"Middle East & North Africa",
"South Asia")) %>%
select(-Indicator.Code) %>%
pivot_longer(cols = 4:65,
names_to = "year",
values_to = "percentage") %>%
mutate(year = str_remove(year, "X"),
percentage = round(percentage, 2)) %>%
filter(!is.na(percentage))
# plot it
seats$tooltip <-
glue_data(seats,
"Region: {Country.Name}",
"\nYear: {year}",
"\nPercentage: {paste0(seats$percentage, '%', sep = '')}")
ggplotly(
seats %>%
ggplot(aes(x = year,
y = percentage,
group = Country.Name,
color = Country.Name)) +
geom_line(aes(text = tooltip)) +
geom_point(aes(text = tooltip),
size = 0.7) +
scale_color_brewer(palette = "Set1") +
scale_y_continuous(breaks = seq(0, 30, by = 5)) +
theme_light() +
theme(axis.text.x = element_text(angle = 90, size = 8),
panel.grid.major.x = element_blank()) +
labs(x = NULL,
y = "Percentage %",
title = "Proportion of seats held by women in national parliaments (%)",
color = "Regions"),
tooltip = "text"
) %>%
layout(annotations = list(x = 0.1,
y = -0.15,
text = "Source: World Bank",
showarrow = FALSE,
xref = "paper",
yref = "paper",
xanchor = "right",
yanchor = "auto",
xshift = 0,
yshift = 0,
font = list(size = 8)
)) %>%
config(displayModeBar = FALSE,
displaylogo = FALSE)